<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>供应商编辑页</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/stylesheets/base.css">
  <script src="/layui/layui.js"></script>
  <script src="/javascripts/jquery.js"></script>
  <script src="/javascripts/ajax.js"></script>
</head>
<body>
<form id="editForm" class="layui-form" style="margin: 10px">
  <input type="hidden" id="id" name="id">
  <div class="layui-form-item">
    <label class="layui-form-label" for="name"><span class="red">*</span>供应商名称</label>
    <div class="layui-input-block">
      <input type="text" name="name" id="name" placeholder="请输入供应商名称" class="layui-input" lay-verify="required">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="contacts"><span class="red">*</span>联系人</label>
    <div class="layui-input-block">
      <input type="text" name="contacts" id="contacts" placeholder="请输入联系人" class="layui-input" lay-verify="required">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="phone"><span class="red">*</span>联系方式</label>
    <div class="layui-input-block">
      <input type="text" name="phone" id="phone" placeholder="请输入联系方式" class="layui-input" lay-verify="required|phone">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="address">地址</label>
    <div class="layui-input-block">
      <textarea class="layui-textarea" name="address" id="address" placeholder="请输入地址"></textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="role"><span class="red">*</span>状态</label>
    <div class="layui-input-block">
      <select name="status" id="status">
        <option value="1">可用</option>
        <option value="2">禁用</option>
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="logo"><span class="red">*</span>logo</label>
    <input type="hidden" name="logo" id="logo">
    <div class="layui-input-block">
      <div class="upload-group">
        <div class="upload-btn">
          <button type="button" class="layui-btn layui-bg-orange" id="uploadBtn">上传图片</button>
        </div>
        <div class="image-group" id="logoDiv">

        </div>
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label"></label>
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-filter="submitBtn" lay-submit>提交</button>
    </div>
  </div>
</form>
</body>
<script>
  const {form,upload} = layui;
  $(function () {
    const id = location.search.substring(location.search.indexOf("=")+1);
    if(id){
      ajax.get("/supplier/info/"+id).then(data=>{
        $("#editForm input,select,textarea").each((index,item)=>{
          const name = $(item).prop("id");
          for(let key in data){
            if(name === key){
              $("#"+name).val(data[key]);
            }
          }
        })
        createLogo(data.logo)
        form.render();
      })
    }

    upload.render({
      elem:"#uploadBtn",
      url:"/upload",
      data:{type:"logo"},
      done(res){
        if(res.code === 200){
          createLogo(res.data)
        }else{
          layer.msg('上传失败！',{icon:5})
        }
      }
    });

    form.on("submit(submitBtn)",function (data) {
      ajax.post("/supplier/update",data.field).then(()=>{
        layer.alert(`${id?"修改":"添加"}成功！`,{icon:6},function (){
          closeWin();
          parent.reloadTable();
        })
      })
      return false;
    })
  })

  function closeWin() {
    const index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
  }

  function createLogo(src) {
    $("#logo").val(src);
    $("#logoDiv").empty().append(`<div class="image-item">
            <div class="image-view">
              <img src="${src}" alt="">
            </div>
            <div class="image-opt">
              <i class="layui-icon layui-icon-search" onclick="showImage(this)"></i>
<!--              <i class="layui-icon layui-icon-delete"></i>-->
            </div>
          </div>`)
  }

  function showImage(_this) {
    const src = $(_this).parents(".image-item").find("img").prop("src");
    layer.photos({
      photos:{
        data:[{
          src
        }]
      }
    })
  }
</script>
</html>
